﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.OleDb;
using System.Data;
using Microsoft.Office.Interop.Excel;

namespace ClassLibrary
{
    class Excel
    {

        public static System.Data.DataTable LoadDataFromExcel(string filePath)
        {
            try
            {
                string strConn;
                strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
                OleDbConnection OleConn = new OleDbConnection(strConn);
                OleConn.Open();
                String sql = "SELECT * FROM  [Sheet1$]";

                OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
                DataSet OleDsExcle = new DataSet();
                OleDaExcel.Fill(OleDsExcle, "Sheet1");
                OleConn.Close();
                return OleDsExcle.Tables[0];
            }
            catch (Exception err)
            {
                throw new Exception("数据绑定Excel失败!失败原因" + err.Message);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="excelTable">数据表</param>
        /// <param name="filePath">保存文件名称，包含全部路径</param>
        /// <param name="alExpFieldName">不需要导出的字段名</param>
        /// <returns></returns>
        public static bool SaveDataTableToExcel(System.Data.DataTable excelTable, string fileName, ArrayList alExpFieldName)
        {
            Microsoft.Office.Interop.Excel.Application app =
                new Microsoft.Office.Interop.Excel.ApplicationClass();
            try
            {
                app.Visible = false;
                Workbook wBook = app.Workbooks.Add(true);
                Worksheet wSheet = wBook.Worksheets[1] as Worksheet;
                if (excelTable.Rows.Count > 0)
                {
                    int row = 0;
                    row = excelTable.Rows.Count;
                    int col = excelTable.Columns.Count;
                    for (int i = 0; i < row; i++)
                    {
                        for (int j = 0; j < col; j++)
                        {
                            //if (alExpFieldName.Contains(excelTable.Columns[j].ColumnName.ToString().ToLower()))
                            //{
                            //    continue; 
                            //}
                            string str = excelTable.Rows[i][j].ToString();
                            wSheet.Cells[i + 2, j + 1] = str;
                        }
                    }
                }

                int size = excelTable.Columns.Count;
                for (int i = 0; i < size; i++)
                {
                    wSheet.Cells[1, 1 + i] = excelTable.Columns[i].ColumnName;
                }
                //设置禁止弹出保存和覆盖的询问提示框 
                app.DisplayAlerts = false;
                app.AlertBeforeOverwriting = false;
                //保存工作簿 
                wBook.Save();
                //保存excel文件 
                app.Save(fileName);
                app.SaveWorkspace(fileName);
                app.Quit();
                app = null;
                return true;
            }
            catch (Exception err)
            {
                throw new Exception("导出Excel出错！错误原因：" + err.Message);
            }
            finally
            {
            }
        }

    }
}
